package com.xxxcompany.world.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xxxcompany.world.entity.PageBean;
import com.xxxcompany.world.service.ICityService;
import com.xxxcompany.world.service.ICountryService;
import com.xxxcompany.world.service.impl.CityServiceImpl;
import com.xxxcompany.world.service.impl.CountryServiceImpl;

@WebServlet("/city2")
public class CityServlet2 extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter pw = resp.getWriter();

		// 1. 接收参数
		String pageStr = req.getParameter("page");
		if (pageStr == null) {
			pageStr = "1";
		}
		int page = Integer.parseInt(pageStr);
		String countStr = req.getParameter("count");
		if (countStr == null) {
			countStr = "10";
		}
		int count = Integer.parseInt(countStr);

		// 2. 调用service
		ICityService cityService = new CityServiceImpl();
		PageBean pageBean = cityService.findByPage(page, count);
		System.out.println("pageBean:" + pageBean);
		req.setAttribute("pageBean", pageBean);

		// 3. 请求转发
		req.getRequestDispatcher("/WEB-INF/city2.jsp").forward(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}